﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PC.AzureMultiApp.Storage;
using PC.AzureMultiApp.Core.Models;
using PC.AzureMultiApp.Core.Helpers;

namespace PC.AzureMultiApp.Core.Applications
{
    public static class Applications
    {
        public static IEnumerable<ApplicationModel> GetAll()
        {
            ApplicationDataSource dataSource = new ApplicationDataSource();

            return dataSource.GetAll()
                            .Select(app => Conversions.GetApplicationModel(app));

        }

        public static void Insert(ApplicationModel app)
        {
            ApplicationDataSource dataSource = new ApplicationDataSource();

            dataSource.Insert(new AzureApplicationEntity()
            {
                ApplicationName = app.ApplicationName,
                ApplicationUrl = app.ApplicationUrl,
                BlobUrl = app.BlobUrl,
                Name = app.Name,
                Label = app.Label,
                Version = app.Version,
                Status = GlobalDef.Deploying,
                UniqueId = app.UniqueId,
                IsWebDeploy = app.IsWebDeploy
            });
        }

        public static void Edit(string id, ApplicationModel app)
        {
            ApplicationDataSource dataSource = new ApplicationDataSource();

            dataSource.Update(id, new AzureApplicationEntity()
            {
                ApplicationName = app.ApplicationName,
                ApplicationUrl = app.ApplicationUrl,
                BlobUrl = app.BlobUrl,
                Name = app.Name,
                Label = app.Label,
                Version = app.Version,
                Status = string.Empty,
                UniqueId = id,
                IsWebDeploy = app.IsWebDeploy
            });
        }

        public static void Delete(string id)
        {
            ApplicationDataSource dataSource = new ApplicationDataSource();
            var apps = dataSource.GetAll().ToList();

            if (apps != null)
            {
                AzureApplicationEntity app = apps.Where(c => c.UniqueId.Equals(id, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

                if (app != null)
                {
                    DeploymentManager.DeleteDeployment(app.UniqueId, app.ApplicationName, app.IsWebDeploy);
                    dataSource.Delete(app);
                }
            }
        }

        public static ApplicationModel GetById(string id)
        {
            ApplicationDataSource dataSource = new ApplicationDataSource();

            ApplicationModel customer = dataSource.GetAll()
                                            .Where(c => c.UniqueId.Equals(id, StringComparison.OrdinalIgnoreCase))
                                            .Select(c => Conversions.GetApplicationModel(c)
                                            ).FirstOrDefault();
            return customer;
        }
    }
}
